<?php
//前面这段是策略模式例子
/*abstract class Strategy{
    public abstract function AlgorithmInterface();
}

class ConcreteStrategyA extends  Strategy{
    public function AlgorithmInterface()
    {
        echo '算法实现A';
    }
}

class ConcreteStrategyB extends  Strategy{
    public function AlgorithmInterface()
    {
        echo '算法实现B';
    }
}

class ConcreteStrategyC extends  Strategy{
    public function AlgorithmInterface()
    {
        echo '算法实现C';
    }
}

//context,用一个ConcreteStrategy来配置,维护一个对Strategy的引用

class Context{

    public $strategy;
    public function __construct(Strategy $strategy)
    {
        $this->strategy = $strategy;
    }

    //上下文接口
    public function ContextInterface(){
        $this->strategy->AlgorithmInterface();
    }

}

//客户端实现
$context = new Context(new ConcreteStrategyA());
$context->ContextInterface();

$context = new Context(new ConcreteStrategyB());
$context->ContextInterface();*/

//策略模式的应用
abstract class CashSuper{
    public abstract function acceptCash($money);
}

/*
 * 不打折的时候
 */
class CashNormal extends CashSuper{
    public function acceptCash($money)
    {
        return $money;
    }
}

/*
 * 折扣的时候
 */
class CashRebate extends CashSuper{
    private $moneyRebate = 1;

    public function __construct($moneyRebate)
    {
        $this->moneyRebate = $moneyRebate;
    }

    public function acceptCash($money)
    {
        return $money * $this->moneyRebate;
    }
}

/*
 * 返点的时候
 */
class CashReturn extends CashSuper{
    private $moneyCondition = 0;
    private $moneyReturn = 0;
    public function __construct($moneyCondition,$moneyReturn)
    {
        $this->moneyCondition = $moneyCondition;
        $this->moneyReturn = $moneyReturn;
    }

    public function acceptCash($money)
    {
        $res = $money;
        if($money > $this->moneyCondition){
            $res = $money - floor($money/$this->moneyCondition) * $this->moneyReturn;
        }
        return $res;
    }
}

/*class CashContext{
    private $CashSuper;
    public function __construct(CashSuper $super){
        $this->CashSuper = $super;
    }

    public function getResult($money){
        return $this->CashSuper->acceptCash($money);
    }
}

//客户端的使用
$context = 0;
//通过客户端选择哪种折扣
$type = '打8折';
switch($type){
    case '正常收费':
        $context = new CashContext(new CashNormal());
        break;
    case '满300返100':
        $context = new CashContext(new CashReturn(300,100));
        break;
    case '打8折':
        $context = new CashContext(new CashRebate(0.8));
        break;
}
//用户输入的单价和数量
$num = 2;
$price = 10;
echo $context->getResult($num*$price);*/

//改进 不要在客户端去判断收费模式
class CashContext{
    private $CashSuper;
    public function __construct($type){
        switch($type){
            case '正常收费':
                $this->CashSuper = new CashNormal();
                break;
            case '满300返100':
                $this->CashSuper = new CashReturn(300,100);
                break;
            case '打8折':
                $this->CashSuper = new CashRebate(0.8);
                break;
        }
    }

    public function getResult($money){
        return $this->CashSuper->acceptCash($money);
    }
}

//客户端使用
//$cashContext = new CashContext('打8折');
//$total = $cashContext->getResult(2*10);
//echo $total;


